package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.Cart;
import com.neusoft.elmboot.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/carts")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/list")
    public List<Cart> listCart(@ModelAttribute Cart cart) throws Exception {
        return cartService.listCart(cart);
    }

    @PostMapping
    public ResponseEntity<?> saveCart(@RequestBody Cart cart) {
        if (cart.getUserId() == null || cart.getBusinessId() == null || cart.getFoodId() == null) {
            return ResponseEntity.badRequest().body("userId、businessId、foodId 均不能为空");
        }
        int rows = cartService.saveCart(cart);
        return rows > 0 ? ResponseEntity.ok(rows)
                        : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("插入失败");
    }

    @PutMapping
    public int updateCart(@ModelAttribute Cart cart) throws Exception {
        return cartService.updateCart(cart);
    }

    @DeleteMapping
    public int removeCart(@ModelAttribute Cart cart) throws Exception {
        return cartService.removeCart(cart);
    }
}